package com.ffcs.web.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ffcs.web.daos.implementations.WechatCanyueyueDao;
import com.ffcs.web.model.entity.WechatCanyueyue;
import com.ffcs.web.utils.XingqiUtil;

@Service
public class WechatCanyueyueService {

	@Autowired
	private WechatCanyueyueDao wechatCanyueyueDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<WechatCanyueyue> getToday(String counseTel,String bookDay) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String bookTimes="";
		if("100".equals(bookDay)){
			bookTimes=sdf.format(new Date());
		}else{
			int xingqi=Integer.parseInt(XingqiUtil.getWeekOfDate(new Date()));
			int bookXingqi=Integer.parseInt(bookDay);
			if(xingqi==bookXingqi){
				bookTimes=sdf.format(new Date());
			}else if(xingqi==(bookXingqi+1)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +6);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi+2)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +5);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi+3)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +4);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi+4)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +3);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi+5)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +2);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi+6)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +1);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-1)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +1);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-2)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +2);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-3)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +3);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-4)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +4);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-5)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +5);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else if(xingqi==(bookXingqi-6)){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, +6);
				Date date=calendar.getTime();
				bookTimes=sdf.format(date);
			}else{
				bookTimes=sdf.format(new Date());
			}
		}
		System.out.println(bookTimes);
		WechatCanyueyue canyueyueExample=new WechatCanyueyue();
		canyueyueExample.setCounseTel(counseTel);
		canyueyueExample.setCanDate(bookTimes);
		canyueyueExample.setStatus("1");
		List<WechatCanyueyue> canYuyueList=wechatCanyueyueDao.findByExample(canyueyueExample);
		return canYuyueList;
	}

	@Transactional
	public void setCanYuyue(String counseTel, String totalTime, String week) {
		int xingqi=Integer.parseInt(XingqiUtil.getWeekOfDate(new Date()));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		int bookXingqi=Integer.parseInt(week);
		String bookTimes="";
		if(xingqi==bookXingqi){
			bookTimes=sdf.format(new Date());
		}else if(xingqi==(bookXingqi+1)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +6);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi+2)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +5);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi+3)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +4);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi+4)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +3);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi+5)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +2);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi+6)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +1);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-1)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +1);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-2)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +2);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-3)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +3);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-4)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +4);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-5)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +5);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else if(xingqi==(bookXingqi-6)){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, +6);
			Date date=calendar.getTime();
			bookTimes=sdf.format(date);
		}else{
			bookTimes=sdf.format(new Date());
		}
		String sql="delete from wechat_canYuyue where canDate='"+bookTimes+"' and counseTel='"+counseTel+"'";
		System.out.println(sql);
		jdbcTemplate.update(sql);
		String canTimes[]=totalTime.split(",");
		for (String canTime : canTimes) {
			if(!"".equals(canTime)){
				String sql2="insert into wechat_canYuyue (canDate,canTime,status,counseTel) values('"+bookTimes+"','"+canTime+"',1,'"+counseTel+"')";
				System.out.println(sql2);
				jdbcTemplate.update(sql2);
			}
		}
	}
}
